# This script performs link-predictive analysis of the PE network
# Last edited: 9/24/2013
# Edited by: Bruce Desmarais

# Read in network data
load("~/Dropbox/professional/Research/Active/SenPE/Data/cleanData/netList.RData")
# amat is the adjacency matrix recording the number of events both attend
# vdat is the vertex dataset containing the variables in DSMKdata

fowler <- read.csv("~/Dropbox/professional/Research/Active/SenPE/Data/SH.csv",stringsAsFactors=F)



fowlerMissing <- NULL

for(i in 1:10){
	if(i ==9){
	ind <- which(netList[[i]]$vdat$ICPSR==95407)	
	netList[[i]]$amat <- netList[[i]]$amat[-ind,-ind]
	netList[[i]]$vdat <- netList[[i]]$vdat[-ind,]
	}
	vdati <- netList[[i]]$vdat
	fowleri <- subset(fowler,fowler$congress==as.numeric(names(netList)[[i]]))
	findid <- match(vdati$ICPSR,fowleri$ids)
	missing <- vdati[which(is.na(findid)),c("Congress","ICPSR","Name")]
	fowlerMissing <- rbind(fowlerMissing,missing)
}

names(fowler) <- paste("fow_",names(fowler),sep="")

for(i in 1:10){
	vdati <- netList[[i]]$vdat[,1:25]
	fowleri <- subset(fowler,fowler$fow_congress==as.numeric(names(netList)[[i]]))
	findid <- match(vdati$ICPSR,fowleri$fow_ids)
	vdati <- data.frame(vdati,fowleri[findid,])
	netList[[i]]$vdat <- vdati
}


save(list="netList",file="~/Dropbox/professional/Research/Active/SenPE/Data/cleanData/netList.RData")

##### Build Matching Cosponsorship Network ####
netListC <- list()
fstub <- names(netList)
for(i in 1:10){
	stubi <- fstub[i]
	stubib <- stubi
	if(nchar(stubi)==2) stubib <- paste("0",stubi,sep="")
	memfile <- paste("~/Dropbox/professional/Research/Active/SenPE/Data/cosponsorship2010/senate_members/",stubi,"_senators.txt",sep="")
	matfile <- paste("~/Dropbox/professional/Research/Active/SenPE/Data/cosponsorship2010/senate_matrices/",stubib,"_senmatrix.txt",sep="")
	senmat <- read.csv(matfile,header=F)
	senmem <- read.csv(memfile,header=F)
	linesi <- match(netList[[i]]$vdat$ICPSR,senmem[,3])
	amati <- matrix(0,length(linesi),length(linesi))
	senmat[senmat==2] <- 1
	senmat <- senmat == 1
	for(j in 2:length(linesi)){
		for(k in 1:(j-1)){
			amati[j,k] <- amati[k,j] <- sum(senmat[linesi[j],]*senmat[linesi[k],])
		}
		print(j)
	}
	netListC[[i]] <- list(amat=amati,vdat=netList[[i]]$vdat)
	print(i)
}

save(list="netListC",file="~/Dropbox/professional/Research/Active/SenPE/Data/cleanData/netListC.RData")
